//
//  loginRegisterViewController.m
//  DuckAndDoc
//
//  Created by 蓝水软件 on 2017/5/14.
//  Copyright © 2017年 苏州蓝水有限公司. All rights reserved.
//

#import "LoginRegisterViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
@interface LoginRegisterViewController ()
@property (nonatomic,strong) UIButton *exChangeButton;
@property (nonatomic,strong) LoginViewController *loginController;
@property (nonatomic,strong) RegisterViewController *registerController;
@property (nonatomic,strong) UIViewController *currentViewController;
@property (nonatomic,assign) BOOL isLoginController;
@end

@implementation LoginRegisterViewController
- (void)viewWillAppear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToLogin) name:@"pushToLogin" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"pushToLogin" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
   
}



- (void)setUI{
    [self addChildViewController:self.loginController];
    [self.view addSubview:self.loginController.view];
    self.currentViewController = self.loginController;
    [self addChildViewController:self.registerController];
    [self.view addSubview:self.currentViewController.view];
    [self.view addSubview:self.exChangeButton];
    _isLoginController = YES;
    [self.exChangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
}

#pragma mark -- buttonAction
- (void)changeController{
    UIViewController *toController;
    UIViewController *fromController = self.currentViewController;
    NSString *buttonTitle;
    if (_isLoginController) {
        toController = self.registerController;
        buttonTitle = LocalizedStrFrom(@"loginOfChina");
    }else{
        toController = self.loginController;
        buttonTitle = LocalizedStrFrom(@"registerOfChina");
    }
    [self transitionFromViewController:fromController toViewController:toController duration:1.0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
        [self.view bringSubviewToFront:self.exChangeButton];
        [self.exChangeButton setTitle:buttonTitle forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
        self.currentViewController = toController;
        if (self.currentViewController == self.loginController) {
            self.isLoginController = YES;
        }else{
            self.isLoginController = NO;
        }
    }];
}

- (void)pushToLogin{
    [self changeController];
}

#pragma mark -- 懒加载
- (LoginViewController *)loginController{
    if (_loginController == nil) {
        _loginController = [LoginViewController new];
    }
    return _loginController;
}

- (RegisterViewController *)registerController{
    if (_registerController == nil) {
        _registerController = [RegisterViewController new];
    }
    return _registerController;
}

- (UIButton *)exChangeButton{
    if (_exChangeButton == nil) {
        _exChangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_exChangeButton setTitle:LocalizedStrFrom(@"registerOfChina") forState:UIControlStateNormal];
        [_exChangeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _exChangeButton.backgroundColor = [UIColor clearColor];
        [_exChangeButton addTarget:self action:@selector(changeController) forControlEvents:UIControlEventTouchUpInside];
    }
    return _exChangeButton;
}


@end
